/*global Qiniu */
/*global plupload */
/*global FileProgress */
/*global hljs */


$(function() {
    var hasVideo = 0; //是否已经有video
    var allowType = 'video';
    var errorMsg = '只能上传视频';
    var curFileType = '';
    var isContinue = 1;
    var $fileInfo = '';
    var domain = 'http://cdn.ajplaychina.com/';

    var uploader = Qiniu.uploader({
        runtimes: 'html5,flash,html4',
        browse_button: 'pickfiles',
        container: 'container',
        drop_element: 'container',
        max_file_size: '1000mb',
        flash_swf_url: '/Public/plupload/Moxie.swf',
        dragdrop: true,
        chunk_size: '4mb',
        multi_selection: !(mOxie.Env.OS.toLowerCase() === "ios"),
        uptoken_url: '/content/qiniu/uptoken',
        domain: domain,
        get_new_uptoken: true,
        //downtoken_url: '/downtoken',
        unique_names: true,
        save_key: false,
        // x_vars: {
        //     'id': '1234',
        //     'time': function(up, file) {
        //         var time = (new Date()).getTime();
        //         // do something with 'time'
        //         return time;
        //     },
        // },
        auto_start: true,
        //log_level: 5,
        init: {
            'FilesAdded': function(up, files) {

                plupload.each(files, function(file) {
                    $fileInfo = file;
                    curFileType = file.type;

                    if (curFileType.indexOf(allowType) == -1) {
                        layer.alert(errorMsg);
                        isContinue = 0;
                        return;
                    } else {

                        isContinue = 1;
                        $('table').show();
                        $('#success').hide();
                    }
                    var progress = new FileProgress(file, 'fsUploadProgress');
                    progress.setStatus("等待...");
                    progress.bindUploadCancel(up);

                });
                if (!isContinue) {
                    return;
                }

            },
            'BeforeUpload': function(up, file) {
                if (!isContinue) {
                    return;
                }
                var progress = new FileProgress(file, 'fsUploadProgress');
                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                if (up.runtime === 'html5' && chunk_size) {
                    progress.setChunkProgess(chunk_size);
                }
            },
            'UploadProgress': function(up, file) {
                if (!isContinue) {
                    return;
                }
                var progress = new FileProgress(file, 'fsUploadProgress');
                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                progress.setProgress(file.percent + "%", file.speed, chunk_size);
            },
            'UploadComplete': function() {
                if (!isContinue) {
                    return;
                }
                $('#success').show();
            },
            'FileUploaded': function(up, file, info) {
                if (!isContinue) {
                    return;
                }
                var progress = new FileProgress(file, 'fsUploadProgress');
                progress.setComplete(up, info);
                var $info = $.parseJSON(info);
                var domain = up.getOption('domain');
                url = domain + encodeURI($info.key);
                //var link = domain + $info.key;
                var datajson = {
                    "filename": $fileInfo.name,
                    "type": $fileInfo.type,
                    "size": $fileInfo.size,
                    "key": $info.key,
                    "url": url
                };
                $.post('/content/media/upload_post', datajson, function(data) {
                    if (data.status) {
                        $('#videoId').val(data.data.id);
                        $('#showVideo').attr('src', '/material/media/play/id/' + data.data.id);
                        $('#showVideoLink').attr('href', '/material/media/play/id/' + data.data.id);
                        $('#videoUrl').val(data.data.url);
                        var vframe = $('#vframe').val();
                        if (vframe <= 0) vframe = 5;
                        $('#videoImg').attr('src', data.data.url + "?vframe/jpg/offset/" + vframe);
                    }
                });
            },
            'Error': function(up, err, errTip) {
                if (!isContinue) {
                    return;
                }
                $('table').show();
                var progress = new FileProgress(err.file, 'fsUploadProgress');
                progress.setError();
                progress.setStatus(errTip);
            },
            'Key': function(up, file) {
                var key = "";
                // do something with key
                return key
            }
        }
    });

    /*
    uploader.bind('FileUploaded', function() {
    	//alert('hello man,a file is uploaded');
        console.log('hello man,a file is uploaded');
    });
    */
    $('#container').on(
        'dragenter',
        function(e) {
            e.preventDefault();
            $('#container').addClass('draging');
            e.stopPropagation();
        }
    ).on('drop', function(e) {
        e.preventDefault();
        $('#container').removeClass('draging');
        e.stopPropagation();
    }).on('dragleave', function(e) {
        e.preventDefault();
        $('#container').removeClass('draging');
        e.stopPropagation();
    }).on('dragover', function(e) {
        e.preventDefault();
        $('#container').addClass('draging');
        e.stopPropagation();
    });

    $('body').on('click', 'table button.btn', function() {
        $(this).parents('tr').next().toggle();
    });


    var getRotate = function(url) {
        if (!url) {
            return 0;
        }
        var arr = url.split('/');
        for (var i = 0, len = arr.length; i < len; i++) {
            if (arr[i] === 'rotate') {
                return parseInt(arr[i + 1], 10);
            }
        }
        return 0;
    };



});